/* Copyright (c) 2010-2020 Vadim Surov.
 */
package com.srv.client;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.srv.client.event.StudioEvent;
import com.srv.client.event.StudioEventHandler;
import com.srv.client.event.HomeEvent;
import com.srv.client.event.HomeEventHandler;
import com.srv.client.event.RunEvent;
import com.srv.client.event.RunEventHandler;
import com.srv.client.event.SessionEvent;
import com.srv.client.event.SessionEventHandler;
import com.srv.client.presenter.Presenter;
import com.srv.client.presenter.BasePresenter;
import com.srv.client.view.StudioView;
import com.srv.client.view.HomeView;
import com.srv.client.view.RunView;
import com.srv.client.view.SessionView;
import com.srv.shared.LoginInfo;
import com.srv.shared.ServiceAsync;

public class AppController implements Presenter, ValueChangeHandler<String> {
	private HasWidgets container;
	private final HandlerManager eventBus; 
	private Presenter presenter;
	private LoginInfo loginInfo = null;

	public AppController() {

		eventBus = Registry.get(Oneortwo.EVENTBUS);
		
		bind();
		
		final ServiceAsync service = Registry.get(Oneortwo.SERVICE);
		// Check login status using login service.
		service.login("sessionKey", "runKey",  new AsyncCallback<LoginInfo>() {
			public void onFailure(Throwable error) {
				AppController.this.loginInfo = null;
			}
			public void onSuccess(LoginInfo loginInfo) {
				AppController.this.loginInfo = loginInfo;
				AppController.this.presenter.onLogin(loginInfo);
			}	    
		});
		
	}

	private void bind() {
		History.addValueChangeHandler(this);

		eventBus.addHandler(HomeEvent.TYPE,
				new HomeEventHandler() {
			@Override
			public void onHome(HomeEvent homeEvent) {
				doHome();
			}
		});  

		eventBus.addHandler(StudioEvent.TYPE,
				new StudioEventHandler() {
			@Override
			public void onDesign(StudioEvent designEvent) {
				doDesign();
			}
		});  

		eventBus.addHandler(SessionEvent.TYPE,
				new SessionEventHandler() {
			@Override
			public void onSession(SessionEvent sessionEvent) {
				doSession();
			}
		});  

		eventBus.addHandler(RunEvent.TYPE,
				new RunEventHandler() {
			@Override
			public void onRun(RunEvent runEvent) {
				doRun();
			}
		});  
	}

	private void doHome() {
		History.newItem("home", false);
		presenter = new BasePresenter(new HomeView("home", loginInfo));
		presenter.go(container);
	}

	private void doDesign() {
		History.newItem("studio", false);
		presenter = new BasePresenter(new StudioView("studio", loginInfo));
		presenter.go(container);
	}

	private void doSession() {
		History.newItem("session", false);
		presenter = new BasePresenter(new SessionView("session", loginInfo));
		presenter.go(container);
	}

	private void doRun() {
		History.newItem("run", false);
		presenter = new BasePresenter(new RunView("run", loginInfo));
		presenter.go(container);
	}

	public void go(final HasWidgets container) {
		this.container = container;

		if ("".equals(History.getToken())) {
			History.newItem("studio");
		}
		else {
			History.fireCurrentHistoryState();
		}
	}

	public void onValueChange(ValueChangeEvent<String> event) {
		String token = event.getValue();

		if (token != null) {
			presenter = null;

			if (token.equals("home")) {
				presenter = new BasePresenter(new HomeView("home", loginInfo));
			}
			else if (token.equals("studio")) {
				presenter = new BasePresenter(new StudioView("studio", loginInfo));
			}
			else if (token.equals("session")) {
				presenter = new BasePresenter(new SessionView("session", loginInfo));
			}
			else if (token.equals("run")) {
				presenter = new BasePresenter(new RunView("run", loginInfo));
			}

			if (presenter != null) {
				presenter.go(container);
			}
		}
	}

	@Override
	public void onLogin(LoginInfo loginInfo) {
	} 
}